package hittegods.gui;

import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import hittegods.classes.*;
import hittegods.classes.Item.Category;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.*;

public class searchElementPnl extends JPanel {

    private JLabel caseNoLabel;
    private JLabel categoryIdLabel;
    private JLabel foundDateLabel;
    private JLabel foundPlaceLabel;
    private JLabel finderNameLabel;
    private JLabel ownerNameLabel;
    private JTextField caseNoTxtField;
    private JTextField foundDateTxtField;
    private JTextField foundPlaceTxtField;
    private JTextField finderNameTxtField;
    private JTextField ownerNameTxtField;
    private JComboBox categoryIdComboBox;
    private JButton searchButton;
    private JPanel searchInputPanel;

    public searchElementPnl() {
        renderPanel();
        actionListeners();
    }

    private void renderPanel() {
        //Overordnet panel størrelse
        this.setLayout(null);

        this.setSize(600, 600);
        //Initialisering af labels
        caseNoLabel = new JLabel("Sagsnummer");
        categoryIdLabel = new JLabel("Kategori");
        foundDateLabel = new JLabel("Funden dato");
        foundPlaceLabel = new JLabel("Fundet sted");
        finderNameLabel = new JLabel("Finderens navn");
        ownerNameLabel = new JLabel("Navn på ejer");
        //Initialisering af textfields
        caseNoTxtField = new JTextField();
        foundDateTxtField = new JTextField();
        foundPlaceTxtField = new JTextField();
        finderNameTxtField = new JTextField();
        ownerNameTxtField = new JTextField();
        //Initialisering af knapper
        searchButton = new JButton("Søg");
        searchButton.setName("searchButton");
        //ComboBox hentes
        categoryIdComboBox = Item.createCatComboBox();
        //Underpanel oprettes
        searchInputPanel = new JPanel(null);

        searchInputPanel.add(caseNoLabel);
        Dimension size = caseNoLabel.getPreferredSize();
        caseNoLabel.setBounds(10, 10, size.width, size.height);
        searchInputPanel.add(caseNoTxtField);
        caseNoTxtField.setBounds(10, 25, 200, 23);

        searchInputPanel.add(categoryIdLabel);
        size = categoryIdLabel.getPreferredSize();
        categoryIdLabel.setBounds(220, 10, size.width, size.height);
        searchInputPanel.add(categoryIdComboBox);
        size = categoryIdComboBox.getPreferredSize();
        categoryIdComboBox.setBounds(220, 25, 200, size.height);
        categoryIdComboBox.setUI(new MotifComboBoxUI());
        //categoryIdComboBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        searchInputPanel.add(foundDateLabel);
        size = foundDateLabel.getPreferredSize();
        foundDateLabel.setBounds(10, 48, size.width, size.height);
        searchInputPanel.add(foundDateTxtField);
        foundDateTxtField.setBounds(10, 63, 200, 23);

        searchInputPanel.add(foundPlaceLabel);
        size = foundPlaceLabel.getPreferredSize();
        foundPlaceLabel.setBounds(220, 48, size.width, size.height);
        searchInputPanel.add(foundPlaceTxtField);
        foundPlaceTxtField.setBounds(220, 63, 200, 23);

        searchInputPanel.add(finderNameLabel);
        size = finderNameLabel.getPreferredSize();
        finderNameLabel.setBounds(10, 86, size.width, size.height);
        searchInputPanel.add(finderNameTxtField);
        finderNameTxtField.setBounds(10, 101, 200, 23);

        searchInputPanel.add(ownerNameLabel);
        size = ownerNameLabel.getPreferredSize();
        ownerNameLabel.setBounds(220, 86, size.width, size.height);
        searchInputPanel.add(ownerNameTxtField);
        ownerNameTxtField.setBounds(220, 101, 200, 23);

        searchInputPanel.add(searchButton);
        size = searchButton.getPreferredSize();
        searchButton.setBounds(420 - size.width, 125, size.width, size.height);
        this.add(searchInputPanel);

        searchInputPanel.setBounds(0, 0, 560, 200);
        repaint();
    }

    private void actionListeners() {
        searchButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<String, String> map = new HashMap<String, String>();
                Category cat = (Category) categoryIdComboBox.getSelectedItem();
                if (!(caseNoTxtField.getText()).equalsIgnoreCase("")) {
                    map.put("caseNo", caseNoTxtField.getText());
                }
                if (!(foundDateTxtField.getText()).equalsIgnoreCase("")) {
                    map.put("foundDate", foundDateTxtField.getText());
                }
                if (!(foundPlaceTxtField.getText()).equalsIgnoreCase("")) {
                    map.put("foundPlace", foundPlaceTxtField.getText());
                }
                if (!(finderNameTxtField.getText()).equalsIgnoreCase("")) {
                    map.put("finderName", finderNameTxtField.getText());
                }
                if (!(ownerNameTxtField.getText()).equalsIgnoreCase("")) {
                    map.put("ownerName", ownerNameTxtField.getText());
                }
                if (cat.getCatId() != 0) {
                    map.put("categoryId", cat.getCatId() + "");
                }

                Item[] items = Search.search(map);
                System.out.println("Search Item count " + items.length);
                ResultDialog dialog = new ResultDialog();
                dialog.parseInfo(items);
                dialog.setVisible(true);
            }
        });
    }
}
